<?php

namespace app\api\Logic\Campusclass;

use app\api\model\CampusClassCourseSignModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

//排课学员签到表
class CampusClassCourseSignLogic
{
    /**
     * 添加排课签到记录
     * @param $params
     * @return int|string
     */
    public function insert($params){
        return CampusClassCourseSignModel::insert([
            'shop_id' => $params['shop_id'] ?? 0,
            'a_id' => $params['a_id'] ?? 0,
            'd_id' => $params['d_id'] ?? 0,
            'user_id' => $params['user_id'] ?? 0,
            'class_id' => $params['class_id'] ?? 0,
            'record_id' => $params['record_id'] ?? 0,
            'week' => $params['week'] ?? '',
            'start_time' => $params['start_time'] ?? '',
            'start_time_txt' => isset($params['start_time']) && !empty($params['start_time']) ? date("Y-m-d H:i:s",$params['start_time']) : '',
            'end_time' => $params['end_time'] ?? '',
            'end_time_txt' => isset($params['end_time']) && !empty($params['end_time']) ? date("Y-m-d H:i:s",$params['end_time']) : '',
            'teacher_id' => $params['teacher_id'] ?? 0,
            'classroom_id' => $params['classroom_id'] ?? 0,
            'status' => $params['status'] ?? 0,
            'create_time' => time(),
            'teacher_status' => $params['teacher_status'] ?? 0,
            'student_status' => $params['student_status'] ?? 0
        ]);
    }

    /**
     * 获取排课签到信息
     * @param $signId
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function getInfo($signId){
        $info = CampusClassCourseSignModel::where('id',$signId)
            ->with([
                'arrangingInfo' => function ($buildSql) {
                    $buildSql->with([
                        'courseInfo' => function ($childSql) {
                            $childSql->withField('id,name');
                        }
                    ])->withField('id,course_id');
                },
                'dateInfo' => function ($buildSql) {
                    $buildSql->withField('id,practice');
                },
                'teacherInfo' => function ($buildSql) {
                    $buildSql->withField('id,username,nickname');
                },
                'classRoom' => function ($buildSql) {
                    $buildSql->withField('id,name');
                },
                'classInfo' => function ($buildSql) {
                    $buildSql->withField('id,name,class_hour,class_hour');
                },
            ])
            ->find();
        return objToArray($info) ?? [];
    }

    /**
     * 取消预约
     * @param $signId
     * @return CampusClassCourseSignModel
     */
    public function cancel($signId){
        return CampusClassCourseSignModel::where('id',$signId)->update(['delete_time' => time()]);
    }

    /**
     * 返回日期段内涉及的课次
     * @param $userId
     * @param $startTime
     * @param $endTime
     * @param array $status
     * @return array|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getInvolve($userId,$startTime,$endTime,$status = []){
        $query = CampusClassCourseSignModel::where('user_id',$userId)
            ->where(function ($query) use ($startTime,$endTime){
                $query->where(function ($sQuery) use ($startTime, $endTime) {
                    $sQuery->where('start_time', '<=', $startTime)->where('end_time', '>=', $endTime);
                })->whereOr(function ($sQuery) use ($startTime, $endTime) {
                    $sQuery->where('start_time', '>=', $startTime)->where('end_time', '<=', $endTime);
                })->whereOr(function ($sQuery) use ($startTime, $endTime) {
                    $sQuery->where('start_time', '>=', $startTime)->where('start_time', '<=', $endTime);
                })->whereOr(function ($sQuery) use ($startTime, $endTime) {
                    $sQuery->where('end_time', '>=', $startTime)->where('end_time', '<=', $endTime);
                });
            });

        if(!empty($status)){
            $query = $query->whereIn('status',$status);
        }

        $list = $query->with([
                'arrangingInfo' => function ($buildSql) {
                    $buildSql->with([
                            'courseInfo' => function ($childSql) {
                                $childSql->withField('id,name');
                            }
                        ])->withField('id,course_id');
                },
                'teacherInfo' => function ($buildSql) {
                    $buildSql->withField('id,username,nickname');
                },
                'classRoom' => function ($buildSql) {
                    $buildSql->withField('id,name');
                },
                'classInfo' => function ($buildSql) {
                    $buildSql->withField('id,name,class_hour,class_hour');
                },
            ])
            ->field('id,a_id,d_id,user_id,teacher_id,classroom_id,class_id,week,start_time,end_time,status')
            ->select();
        return objToArray($list) ?? [];
    }

    /**
     * 返回学员课次
     * @param $userId
     * @param array $status
     * @return array|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getInvolveByStatus($userId,$status = []){
        $query = CampusClassCourseSignModel::where('user_id',$userId);

        if(!empty($status)){
            $query = $query->whereIn('status',$status);
        }

        $list = $query->with([
                'arrangingInfo' => function ($buildSql) {
                    $buildSql->with([
                        'courseInfo' => function ($childSql) {
                            $childSql->withField('id,name');
                        }
                    ])->withField('id,course_id');
                },
                'teacherInfo' => function ($buildSql) {
                    $buildSql->withField('id,username,nickname');
                },
                'classRoom' => function ($buildSql) {
                    $buildSql->withField('id,name');
                },
                'classInfo' => function ($buildSql) {
                    $buildSql->withField('id,name,class_hour,class_hour');
                },
            ])
            ->field('id,a_id,d_id,user_id,teacher_id,classroom_id,class_id,week,start_time,end_time,status')
            ->select();
        return objToArray($list) ?? [];
    }

    /**
     * 根据签到ID获取签到排课列表
     * @param $signIds
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getListBySignIds($signIds){
        $list = CampusClassCourseSignModel::whereIn('id',$signIds)
            ->with([
                'arrangingInfo' => function ($buildSql) {
                    $buildSql->with([
                        'courseInfo' => function ($childSql) {
                            $childSql->withField('id,name');
                        }
                    ])->withField('id,course_id');
                }
            ])
            ->field('id,a_id,d_id,user_id,class_id,week,start_time,end_time,status')
            ->select();
        return objToArray($list) ?? [];
    }

    /**
     * 修改签到表状态
     * @param $signIds
     * @param $status
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function updateStatus($signIds,$status){
        $signIds = is_array($signIds) ? $signIds : explode(',',$signIds);

        foreach ($signIds as $signId){
            $info = CampusClassCourseSignModel::where('id',$signId)
                ->field('status,old_status')
                ->find();
            $info = objToArray($info) ?? [];

            CampusClassCourseSignModel::where('id',$signId)
                ->update(['old_status' => $info['status'],'status' => $status]);
        }

        return true;
    }
}
